/*
 * Copyright 2005-2025 noear.org and authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.noear.snack4.codec.util;

import org.noear.snack4.annotation.ONodeAttr;
import org.noear.snack4.annotation.ONodeAttrHolder;
import org.noear.snack4.util.Asserts;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;

/**
 *
 * @author noear 2025/10/8 created
 * @since 4.0
 */
public class PropertyMethodWrap implements Property {
    private final Method property;
    private final TypeWrap propertyTypeWrap;

    private final String name;

    private ONodeAttrHolder attr;
    private String nodeName;

    private boolean isReadMode;

    public PropertyMethodWrap(TypeWrap owner, Method property) {
        this.property = property;

        if (property.getReturnType() != void.class) {
            //getter
            this.isReadMode = true;
            this.propertyTypeWrap = TypeWrap.from(GenericUtil.reviewType(property.getGenericReturnType(), getGenericInfo(owner, property)));
        } else {
            //setter
            this.propertyTypeWrap = TypeWrap.from(GenericUtil.reviewType(property.getGenericParameterTypes()[0], getGenericInfo(owner, property)));
        }

        String nameTmp = property.getName().substring(3);
        this.name = nameTmp.substring(0, 1).toLowerCase() + nameTmp.substring(1);
    }

    /**
     * 初始化属性
     *
     */
    protected void initAttr(FieldWrap fieldWrap) {
        ONodeAttr attrAnno = property.getAnnotation(ONodeAttr.class);

        if (attrAnno == null) {
            if (fieldWrap != null) {
                this.attr = fieldWrap.getAttr();
            }
        }

        if (this.attr == null) {
            if (attrAnno != null) {
                this.attr = new ONodeAttrHolder(attrAnno, false);
            } else {
                this.attr = new ONodeAttrHolder(null, false);
            }
        }

        this.nodeName = this.attr.getName();

        if (Asserts.isEmpty(nodeName)) {
            this.nodeName = this.name;
        }
    }

    @Override
    public Object getValue(Object target) throws Exception {
        if (isReadMode) {
            if (property.isAccessible() == false) {
                property.setAccessible(true);
            }

            return property.invoke(target);
        } else {
            return null;
        }
    }

    @Override
    public void setValue(Object target, Object value) throws Exception {
        if (isReadMode == false) {
            if (property.isAccessible() == false) {
                property.setAccessible(true);
            }

            property.invoke(target, value);
        }
    }

    @Override
    public TypeWrap getTypeWrap() {
        return propertyTypeWrap;
    }

    @Override
    public ONodeAttrHolder getAttr() {
        return attr;
    }

    @Override
    public String getOrigName() {
        return name;
    }

    public String getNodeName() {
        return nodeName;
    }

    @Override
    public String toString() {
        return property.toString();
    }

    private static Map<String, Type> getGenericInfo(TypeWrap owner, Method method) {
        if (method.getDeclaringClass() == owner.getType()) {
            return owner.getGenericInfo();
        } else {
            Type superType = GenericUtil.reviewType(owner.getType().getGenericSuperclass(), owner.getGenericInfo());
            return getGenericInfo(TypeWrap.from(superType), method);
        }
    }
}